#include <boot/boot.h>
#include <draw.h>
#include <str.h>

message_t __msg;
vbe_mode_info_t __vbe_info;

int init_draw(u32_t fg, u32_t bg)
{
    u64_t size;
    do
    {
        i32_t d0, d1, d2;
        size = sizeof(vbe_mode_info_t);
        __asm__ __volatile__("cld	\n\t"
                             "rep	\n\t"
                             "movsq	\n\t"
                             "testb	$4,%b4	\n\t"
                             "je	1f	\n\t"
                             "movsl	\n\t"
                             "1:\ttestb	$2,%b4	\n\t"
                             "je	2f	\n\t"
                             "movsw	\n\t"
                             "2:\ttestb	$1,%b4	\n\t"
                             "je	3f	\n\t"
                             "movsb	\n\t"
                             "3:	\n\t"
                             : "=&c"(d0), "=&D"(d1), "=&S"(d2)
                             : "0"(size / 8), "q"(size), "1"(&__vbe_info), "2"(KERNEL_BASE + (VESA_MODE_INFO_DS << 4) + VESA_MODE_INFO_ADDR)
                             : "memory");
    } while (0);
    if (__vbe_info.framebuffer != 0xe0000000)
    {
        while (1)
            ;
    }

    if (__vbe_info.width % 8 != 0)
    {
        while (1)
            ;
    }

    if (__vbe_info.height % 16 != 0)
    {
        while (1)
            ;
    }
    __msg.width = __vbe_info.width;
    __msg.height = __vbe_info.height;
    __msg.bpp = __vbe_info.bpp / 8;
    __msg.pitch = __vbe_info.pitch;
    __msg.x = 0;
    __msg.y = 0;
    __msg.fg = fg;
    __msg.bg = bg;
    __msg.addr = 0xffff800001200000;

    return 0;
}